package com.travel.ticket.service.impl;

import com.travel.ticket.entity.SpuRelationAttr;
import com.travel.ticket.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.common.utils.PageUtils;
import com.travel.common.utils.Query;

import com.travel.ticket.dao.AttrDao;
import com.travel.ticket.entity.AttrEntity;
import com.travel.ticket.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAttr(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();

        // 属性对拷
        BeanUtils.copyProperties(attrVo,attrEntity);

        // 将vo的valueSelect 转为String，每个值用","分割
        List<String> valueSelect = attrVo.getSelectValues();
        String value = String.join(",", valueSelect);
        attrEntity.setValueSelect(value);

        AttrDao attrDao = this.baseMapper;
        attrDao.insert(attrEntity);


    }

    /**
     * 获取attr列表
     * @return
     */
    @Override
    public List<AttrEntity> getAttrList() {
        return this.baseMapper.selectList(new QueryWrapper<AttrEntity>());
    }

    /**
     * 获取该spuId与attr的关联实体类
     * @param spuId
     */
    @Override
    public List<SpuRelationAttr> getSpuIdRelationAttr(Integer spuId) {

        List<SpuRelationAttr> spuRelationAttrs = this.baseMapper.getSpuIdRelationAttr(spuId);
        return spuRelationAttrs;
    }


}